package com.hiwhitley.himei.contract.presenter;

import com.hiwhitley.himei.contract.GameContract;
import com.hiwhitley.himei.entity.GameItem;
import com.hiwhitley.himei.entity.GameList;
import com.hiwhitley.himei.mvpframe.rx.RxSchedulers;

import java.util.ArrayList;
import java.util.List;

import rx.Observer;

import static com.hiwhitley.himei.contract.model.GameModel.parseDataByType;

/**
 * Created by hiwhitley on 2016/10/11.
 */

public class GamePresenter extends GameContract.Presenter {

    @Override
    public void loadGames(int offset, int limit) {
        mRxManager.add(mModel.getGames(offset, limit)
                .compose(RxSchedulers.io_main())
                .subscribe(new Observer<GameList>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.onInternetError();
                        mView.onRequestEnd();
                    }

                    @Override
                    public void onNext(GameList gameList) {
                        List<GameItem> itemList = new ArrayList<>();
                        for (GameList.Data data : gameList.getData()) {
                            try {
                                if (data.isIs_pay_for() || data.getPacks().isEmpty())
                                    continue;
                                itemList.add(parseDataByType(data, GameItem.TYPE_HEADER));
                                itemList.add(parseDataByType(data, GameItem.TYPE_POSTER));
                                itemList.add(parseDataByType(data, GameItem.TYPE_DESC));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        mView.showGames(itemList);
                    }
                })

        );

    }
}
